'use strict';
const { Controller } = require('egg');
const Result = require('../util/result');

class TicketController extends Controller {
  async index() {
    const query = {
      offset: this.ctx.query.offset,
      limit: Result.toInt(this.ctx.query.limit),
    };
    const id = this.ctx.query.id;
    const response = await this.ctx.service.ticket.findByBuyerId(id, query);
    this.ctx.body = response;
  }

  async create() {
    const data = this.ctx.request.body;
    const response = await this.ctx.service.ticket.create(data);
    this.ctx.body = response;
  }

  async show() {
    const id = this.ctx.params.id;
    const response = await this.ctx.service.ticket.findOne(id);

    this.ctx.body = response;
  }

  async update() {
    const id = this.ctx.params.id;
    const response = await this.ctx.service.ticket.finishOrder(id);
    this.ctx.body = response;
  }

  async destroy() {
    const id = this.ctx.params.id;
    const response = await this.ctx.service.ticket.destroy(id);
    this.ctx.body = response;
  }
}

module.exports = TicketController;
